package com.dwxt.community.config;

import com.dwxt.common.aop.*;
import com.dwxt.common.config.*;
import com.dwxt.common.exception.*;
import com.dwxt.common.util.*;
import org.springframework.context.annotation.*;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 注入一些公共的处理类
 */
@Configuration
public class BeanConfig {

    @Bean
    public ValidateAspectHandel getValidateAspectHandel() {
        return new ValidateAspectHandel();
    }

    @Bean
    public PartyUtil getPartyUtil() {
        return new PartyUtil();
    }

    @Bean
    public ExceptionHandler getExceptionHandler() {
        return new ExceptionHandler();
    }

    @Bean
    public FeignConfiguration getFeignConfiguration(){
        return new FeignConfiguration();
    }

    @Bean
    public Executor myExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(30);
        executor.setQueueCapacity(30);
        //允许线程的空闲时间60秒：当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("MallExecutor-");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //该方法用来设置线程池中任务的等待时间，如果超过这个时候还没有销毁就强制销毁，以确保应用最后能够被关闭，而不是阻塞住
        executor.setAwaitTerminationSeconds(60);
        //设置为守护线程，当jvm停止后，线程自动停止
        executor.setDaemon(true);
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是由调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
